/*
 * $Id: Coordinate.java 48 2010-01-04 01:48:58Z aaron.day $
 */
package org.crazydays.gameplan.map;


import java.io.Serializable;

/**
 * Coordinate is a simple x,y coordinate on a map.
 */
public class Coordinate
    implements Serializable, Comparable<Coordinate>
{
    /** serial version uid */
    public final static long serialVersionUID =
        "$Id: Coordinate.java 48 2010-01-04 01:48:58Z aaron.day $".hashCode();

    /** x */
    protected int x;

    /** y */
    protected int y;

    /**
     * Coordinate constructor.
     */
    public Coordinate()
    {
        setX(0);
        setY(0);
    }

    /**
     * Coordinate constructor.
     * 
     * @param x X
     * @param y Y
     */
    public Coordinate(int x, int y)
    {
        setX(x);
        setY(y);
    }

    /**
     * Set x.
     * 
     * @param x X
     */
    public void setX(int x)
    {
        this.x = x;
    }

    /**
     * Get x.
     * 
     * @return X
     */
    public int getX()
    {
        return x;
    }

    /**
     * Set y.
     * 
     * @param y Y
     */
    public void setY(int y)
    {
        this.y = y;
    }

    /**
     * Get y.
     * 
     * @return Y
     */
    public int getY()
    {
        return y;
    }

    /**
     * Equals
     * 
     * @param o Object
     * @return Equals
     * @see Object#equals(Object)
     */
    @Override
    public boolean equals(Object o)
    {
        if (o == null) {
            return false;
        }

        if (!(o instanceof Coordinate)) {
            return false;
        }

        Coordinate coord = (Coordinate) o;

        return x == coord.x && y == coord.y;
    }

    /**
     * Hash code.
     * 
     * @return Hash code
     * @see Object#hashCode()
     */
    @Override
    public int hashCode()
    {
        return x * y + x + y;
    }

    /**
     * Compare x coordinate and then y coordinate.
     * 
     * @param o Coordinate
     * @return Comparison
     * @see Comparable#compareTo(Object)
     */
    @Override
    public int compareTo(Coordinate o)
    {
        if (x != o.x) {
            return x - o.x;
        } else {
            return y - o.y;
        }
    }
}
